const superAgent = require('superagent');

const userConfig = require('../../userConfig.json');

const baseUrl = 'https://weibo.com/ajax/statuses';

module.exports = ({ url, method, data }) => {
  const path = baseUrl + url;
  return new Promise((resolve, reject) => {
    superAgent(method, path)
      .query(data)
      .send(data)
      .set('cookie', userConfig.cookie)
      .set('traceparent', userConfig.traceparent)
      .end((err, res) => {
        if (err) {
          console.log(`接口:${path}在请求阶段出错了`);
          reject(err);
        } else {
          if (res.ok) {
            resolve(JSON.parse(res.text));
          } else {
            console.log(`接口:${path}在响应阶段出错了`);
            reject(JSON.parse(res.text));
          }
        }
      });
  });
};
